package cn.cytong.nk0713.services;

import cn.cytong.nk0713.utils.nutz.CustomisedNutDao;
import cn.cytong.rpc.server.NutzRpcService;
import flexjson.JSONSerializer;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.entity.Record;
import org.nutz.trans.Trans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 操作员信息
 */
public class OperatorService extends NutzRpcService {
    private Logger logger = LoggerFactory.getLogger(OperatorService.class);
    private CustomisedNutDao nutDao;

    /**
     * 保存读卡器端口号
     *
     * @param params port    端口号
     *               branch  网点Id
     * @return success true or false
     * @throws Exception
     */
    public String savePort(Map params) throws Exception {
        Map result = new HashMap();
        Integer port = (Integer) params.get("port");
        Object branchId = params.get("branchId");
        try {
            Trans.begin();
            checkNotNull(port, "端口号不可为空");
            checkNotNull(branchId, "网点信息错误");

            logger.info("修改端口号：网点（{}），端口号（{}）", branchId, port);

            nutDao.executeUpdate("update operator set readerPort = ? where serviceBranch_id = ?", port, branchId);
            result.put("success", true);
            Trans.commit();
        } catch (Exception e) {
            Trans.rollback();
            logger.error("error in method OperatorService.savePort", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        } finally {
            Trans.close();
        }
        return new JSONSerializer().serialize(result);
    }

    /**
     * 更改操作员密码
     *
     * @param params pwd1    旧密码
     *               pwd2    旧密码确认
     *               newPwd  新密码
     * @return success true or false
     * @throws Exception
     */
    public String changePwd(Map params) throws Exception {
        Map result = new HashMap();
        String pwd1 = (String) params.get("pwd1");
        String pwd2 = (String) params.get("pwd2");
        String newPwd = (String) params.get("newPwd");

        try {
            Trans.begin();
            checkArgument(StringUtils.isNotBlank(pwd1));
            checkArgument(StringUtils.isNotBlank(pwd2));
            checkArgument(pwd1.equals(pwd2));
            checkArgument(StringUtils.isNotBlank(newPwd));


            Trans.commit();
        } catch (Exception e) {
            Trans.rollback();
            logger.error("error in method OperatorService.changePwd", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        } finally {
            Trans.close();
        }
        return new JSONSerializer().serialize(result);
    }

    /**
     * 获取操作员信息
     *
     * @param params username    操作员用户名
     * @return operator    操作员信息
     * success     true or false
     */
    public String getOperator(Map params) {
        Map result = new HashMap();
        String userName = (String) params.get("userName");
        try {
            Record operator = nutDao.fetchTableRecord("operator", "username = ?", userName);
            checkNotNull(operator, "操作员信息不存在");
            result.put("operator", operator);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("error in method OperatorService.getOperator", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return new JSONSerializer().serialize(result);
    }

}
